package com.loper7.base.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.jaeger.library.StatusBarUtil;

/**
 * @author LOPER7
 * @date 2018/3/14 11:34
 * @Description: 状态栏帮助类
 */

public class StatusBarHelper {

    @ColorInt
    public static int Translucent = -101;

    public static void setStatusBar(Activity activity, @ColorInt int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (color == Translucent) {
                StatusBarUtil.setTranslucentForImageView(activity, 0, null);
                setDarkStatusIcon(activity, false);
            } else {
                StatusBarUtil.setColor(activity, color, 0);
                setDarkStatusIcon(activity, !isDarkColor(color));
            }
        } else {
            if (color == Translucent) {
                StatusBarUtil.setTranslucentForImageView(activity, 55, null);
            } else {
                StatusBarUtil.setColor(activity, color, 55);
            }

        }
    }

    /**
     * 是否使用深色状态栏
     *
     * @param bDark
     */
    public static void setDarkStatusIcon(Activity activity, boolean bDark) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = activity.getWindow().getDecorView();
            if (decorView != null) {
                int vis = decorView.getSystemUiVisibility();
                if (bDark) {
                    vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
                decorView.setSystemUiVisibility(vis);
            }
        }
    }


    public static void setStatusBarViewHeight(Context context, View view, RelativeLayout.LayoutParams layoutParams) {
        layoutParams.height = getStatusBarHeight(context);
        view.setLayoutParams(layoutParams);
    }

    public static void setStatusBarViewHeight(Context context, View view) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        layoutParams.height = getStatusBarHeight(context);
        view.setLayoutParams(layoutParams);
    }

    public static void setStatusBarViewHeight(Context context, View view, int height) {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
        layoutParams.height = height;
        view.setLayoutParams(layoutParams);
    }


    /**
     * 获取状态栏和标题栏高度
     *
     * @param context context
     * @return 状态栏和标题栏高度
     */
    public static int getStatusTitleBarHeight(Context context) {
        return getStatusBarHeight(context) + DisplayUtil.dip2px(context, 50);
    }

    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }

    /**
     * 是否是深色
     *
     * @param color
     * @return
     */
    public static boolean isDarkColor(int color) {
        if (color == StatusBarHelper.Translucent)
            return true;

        int red = (color & 0xff0000) >> 16;
        int green = (color & 0x00ff00) >> 8;
        int blue = (color & 0x0000ff);

        int grayLevel = (int) (red * 0.299 + green * 0.587 + blue * 0.114);

        if (grayLevel >= 192) {
            return false;
        }

        return true;
    }


}
